import React from "react";
import { Route, Redirect } from "react-router-dom";
import { isAuth } from "../../utils/auth";

const AuthRoute = ({ component: Component, ...rest }) => {
  return (
    <Route
      {...rest}
      render={props => {
        const isLogin = isAuth();
        if (isLogin) {
          // 登录了
          return <Component {...props} />;
        } else {
          // 没登录
          return (<Redirect
            to={{
              pathname: "/login",
              state: {form: props.location}
            }}
          />)
        }
      }}
    />
  );
};
export default AuthRoute;
